define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'bootstrap-table-editable'], function ($, undefined, Backend, Table, Form, undefined) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'box/address/housegoods/index',
                    add_url: 'box/address/goods/index?qid=' + Fast.api.query('qid'),
                    edit_url: 'box/address/housegoods/edit',
                    del_url: 'box/address/housegoods/del',
                    multi_url: 'box/address/housegoods/multi',
                    table: 'house_goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'house_goods_id',
                sortName: 'house_goods_id',
                queryParams: function (params) {
                    var options = table.bootstrapTable('getOptions');
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    filter.house_id = Fast.api.query('qid');
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
                columns: [
                    [
                        {field: 'goods_id', title: __('Goods_id')},
                        {
                            field: 'goods_title',
                            title: __('Goods_title'),
                            formatter: Controller.api.formatter.goods_info,
                            operate: 'like',
                            align: 'left',
                            width: '20%'
                        },
                        {field: 'cate_id', title: __('Cate_title'), visible: false, searchList: $.getJSON('goods/cateList')},
                        {field: 'brand_id', title: __('Brand_title'), visible: false, searchList: $.getJSON('goods/brandList')},
                        {field: 'brand_title', title: __('Brand_title'), operate: false},
                        {field: 'cate_title', title: __('Cate_title'), operate: false},
                        {field: 'purchase_price', title: __('Purchase_price'), operate: false},
                        {field: 'goods_price', title: __('Goods_price'), operate: false},
                         {
                            field: 'selling_price',
                            title: __('Selling_price'),
                            editable: {
                                type: 'text'
                            }
                        },
                        {field: 'goods_weight', title: __('Goods_weight'), operate: false},
                        {field: 'sale_num', title: __('Sale_num'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate: false, addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        //自定义栏位,custom是不存在的字段
                        {
                            field: 'custom',
                            title: __('操作'),
                            operate: false,
                            width: '80px',
                            formatter: Controller.api.formatter.custom
                        },
                    ]
                ],
                onEditableSave: function (field, row, oldValue, $el) {
                    $.ajax({
                        type: "post",
                        url: "box/address/housegoods/editnum",
                        data: row,
                        dataType: 'JSON',
                        success: function (data, status) {
                            if (status == "success") {
                                Toastr.success('操作成功');
                            }
                        },
                        error: function () {
                            Toastr.success('编辑失败');
                        },
                    });
                }
            });

            $(table).on("click", "[data-id].btn-delgoods", function (e) {
                e.preventDefault();
                var id = $(this).data("id");

                var that = this;
                Layer.confirm(
                        __('确定删除小区商品吗？'),
                        {icon: 3, title: __('确定删除小区商品吗？'), shadeClose: true},
                        function (index) {
                            $.post('box/address/housegoods/del', {ids: id}, function (res) {
                                table.bootstrapTable('refresh', {});
                            });
                            Layer.close(index);
                        }
                );
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                goods_info: function (value, row, index) {
                    var html = '<div style="width:250px;"><img class="img-sm img-center" src="' + Fast.api.cdnurl(row.goods_logo) + '">&nbsp;';
                    html += row.goods_title + ',' + row.goods_model + ',' + row.goods_spec + '</div>';
                    return html;
                },
                custom: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    var html = '<a class="text-danger btn-delgoods"  style="cursor:pointer" data-url="box/address/housegoods/delgoods"  data-id="' + row.house_goods_id + '"><b>删除</b></a>';

                    return html;
                }
            },
        }
    };
    return Controller;
});